﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using System.Configuration;
using System.Net.Mail;

namespace Avatar.BLL
{
    public class EmailConfig
    {
        #region 私有变量
        /// <summary>
        /// Email.Config在服务器上的地址
        /// </summary>
        private readonly string _EmailFile = HttpContext.Current.Server.MapPath("~/Config/Email.Config");
        /// <summary>
        /// SMTP服务器名称或IP
        /// </summary>
        private string _SMTP;
        /// <summary>
        /// SMTP服务器端口
        /// </summary>
        private string _POST;
        /// <summary>
        /// SMTP邮箱地址
        /// </summary>
        private string _USER;
        /// <summary>
        /// SMTP邮箱密码
        /// </summary>
        private string _PASS;
        /// <summary>
        /// 安全套接字层
        /// </summary>
        private string _SSL;
        /// <summary>
        /// 邮件标题
        /// </summary>
        private string _EmailTitle;
        /// <summary>
        /// 邮件内容
        /// </summary>
        private string _EmailBody;
        #endregion

        #region 属性
        public string FILE
        {
            get { return _EmailFile; }
        }
        public string SMTP
        {
            get { return _SMTP; }
            set { _SMTP = value; }
        }
        public string POST
        {
            get { return _POST; }
            set { _POST = value; }
        }
        public string USER
        {
            get { return _USER; }
            set { _USER = value; }
        }
        public string PASS
        {
            get { return _PASS; }
            set { _PASS = value; }
        }
        public string SSL
        {
            get { return _SSL; }
            set { _SSL = value; }
        }
        public string Title
        {
            get { return _EmailTitle; }
            set { _EmailTitle = value; }
        }
        public string Body
        {
            get { return _EmailBody; }
            set { _EmailBody = value; }
        }
        #endregion

        #region 公共变量
        XmlDocument xml = new XmlDocument();
        // 初始化
        public EmailConfig() {
            //Command.sqlCommand();
            LoadXML();
        }
        #endregion

        /// <summary>
        /// 读取Email.Config文件
        /// </summary>
        public void LoadXML()
        {
            //xml.Load(_EmailFile);
            //_SMTP = xml.SelectSingleNode("Email/SMTP").InnerText;
            //_POST = xml.SelectSingleNode("Email/SMTP_POST").InnerText;
            //_USER = xml.SelectSingleNode("Email/Email_User").InnerText;
            //_PASS = xml.SelectSingleNode("Email/Email_Pwd").InnerText;
            //_SSL = xml.SelectSingleNode("Email/SSL").InnerText;
            _SMTP = "smtp.gmail.com";
            _POST = "587";
            _USER = "lichenyeah@gmail.com";
            _PASS = "lichenchen";
            _SSL = "1";
        }
        /// <summary>
        /// 保存Email.Config文件
        /// </summary>
        /// <returns>返回bool值，真：保存成功；假：保存失败。</returns>
        public bool saveXML()
        {
            try {
                xml.LoadXml(_EmailFile);
                xml.SelectSingleNode("Email/SMTP").InnerText = SMTP;
                xml.SelectSingleNode("Email/SMTP_POST").InnerText = POST;
                xml.SelectSingleNode("Email/Email_User").InnerText = USER;
                xml.SelectSingleNode("Email/Email_Pwd").InnerText = PASS;
                xml.SelectSingleNode("Email/SSL").InnerText = SSL;
                xml.Save(FILE);
                return true;
            } catch {
                return false;
            }
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="toEMail"></param>
        /// <returns></returns>
        public bool sendEmail(string toEMail)
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Host = SMTP;
            smtp.Port = Int32.Parse(POST);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new System.Net.NetworkCredential(USER, PASS);
            if (SSL == "1")
                smtp.EnableSsl = true;
            else
                smtp.EnableSsl = false;

            MailMessage mm = new MailMessage(USER, toEMail);
            mm.Subject = Title;
            mm.Body = Body;
            mm.SubjectEncoding = System.Text.Encoding.Default;
            mm.IsBodyHtml = true;
            try
            {
                smtp.Send(mm);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
